package com.tjsinfo.tjsoa.system.action;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.system.bo.IDepartmentBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.TSystemDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class SelectUserTreeCheckAction   extends Action{
	private IDepartmentBo departmentBo;
	private ISystemUserBo systemUserBo;
	private HttpServletRequest request;
	
	
	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}

	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}

	public IDepartmentBo getDepartmentBo() {
		return departmentBo;
	}

	public void setDepartmentBo(IDepartmentBo departmentBo) {
		this.departmentBo = departmentBo;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			 {
	
		try {
			JSONArray array = new JSONArray();
			String parentid = request.getParameter("node");
			if(!parentid.equals("0")){
				List<TSystemDepartment> list = departmentBo.selectByParentID(parentid);
				for (Iterator iterator = list.iterator(); iterator.hasNext();) {
					TSystemDepartment systemDepartment = (TSystemDepartment) iterator.next();
					List<TSystemUser> systemuserlist =  systemUserBo.selectByDepartmentNotDel(systemDepartment.getId());
					if(systemuserlist.size() > 0)
					{	
						JSONObject object = new JSONObject();
						object.put("id",  systemDepartment.getId().trim());
						object.put("text", systemDepartment.getDeptName());
						object.put("checked", false);
						object.put("isdepartment", true);	
						object.put("leaf", false);
						if(systemDepartment.getIsZhiShu() !=null){
							object.put("isZhiShu", true);
						}else{
							object.put("isZhiShu", false);
						}
						
						
						array.put(object);
					}
			
				}
				List<TSystemUser> systemuserlist =  systemUserBo.selectByDepartmentNotDel(parentid);
				if(systemuserlist.size() > 0)
				{	
					JSONObject object = new JSONObject();
					
					JSONArray systemuserarray = new JSONArray();
					for (TSystemUser systemUser : systemuserlist) {
						if(systemUser != null)
						{
							JSONObject systemUserobject = new JSONObject();
							systemUserobject.put("id",  systemUser.getUserId());
							systemUserobject.put("text", systemUser.getUserName());
							systemUserobject.put("checked", false);
							systemUserobject.put("leaf", true);
							systemUserobject.put("isdepartment", false);
							array.put(systemUserobject);
						}
						
					}
					response.getWriter().print(array);
					return null;
					
				}
				response.getWriter().print(array);
			}else{
				List<TSystemDepartment> list = departmentBo.selectByParentID(parentid);
				for (Iterator iterator = list.iterator(); iterator.hasNext();) {
					TSystemDepartment systemDepartment = (TSystemDepartment) iterator.next();
					List<TSystemUser> systemuserlist =  systemUserBo.selectByDepartmentNotDel(systemDepartment.getId());
					if(systemuserlist.size() > 0)
					{	
						JSONObject object = new JSONObject();
						object.put("id",  systemDepartment.getId().trim());
						object.put("text", systemDepartment.getDeptName());
						object.put("checked", false);
						object.put("isdepartment", true);	
						object.put("leaf", false);
						if(systemDepartment.getIsZhiShu() !=null){
							object.put("isZhiShu", true);
						}else{
							object.put("isZhiShu", false);
						}
						
						
						array.put(object);
					}
			
				}
				response.getWriter().print(array);
			}
			
			return null;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
